<template>
  <div class="userSetContainer">
      <!-- 顶部导航 -->
      <div class="top">
         <div class="left" @touchend="$router.back()">
             <yd-icon name="back" slot="icon" custom  size=".3rem" color="#FFffff"></yd-icon>
             详细资料
         </div>
         <div class="mid">资料设置</div>
      </div>
      <!-- 设置列表 -->
        <div class="setList">
            <div class="item">
                <div class="left">
                    位置
                </div>
                <div class="right" >
                    <yd-switch v-model="position" @touchend.native="poi"></yd-switch>
                </div>
            </div>
            <div class="item" v-if="stateWrite=='focus'">
                <div class="left">
                    运动
                </div>
                <div class="right">
                    <yd-switch v-model="sport" @touchend.native="spo"></yd-switch>
                </div>
            </div>
            <div class="item" v-if="stateWrite=='focus'">
                <div class="left">
                    环境
                </div>
                <div class="right">
                    <yd-switch v-model="weather" @touchend.native="wea"></yd-switch>
                </div>
            </div>
            <div class="item" v-if="stateWrite=='focus'">
                <div class="left">
                    体征
                </div>
                <div class="right">
                    <yd-switch v-model="heart" @touchend.native="hea"></yd-switch>
                </div>
            </div>
            <div class="item" v-if="stateWrite=='focus'">
                <div class="left">
                    睡眠
                </div>
                <div class="right">
                    <yd-switch v-model="sleep" @touchend.native="sle"></yd-switch>
                </div>
            </div>                                                
        </div>
  </div>
</template>
<script>
export default {
  data(){
      return{
          positionid:null,//位置
          position:false,
          sportid:null,
          sport:false,//运动
          weatherid:null,//天气
          weather:false,
          heartid:null,//心率
          heart:false,
          sleepid:null,//睡眠
          sleep:false
      }
  },
  computed:{
      userState(){//用户权限开关
        return this.$store.state.userState
      },
      stateWrite(){
          return this.$store.state.stateWrite
      }
  },
  methods:{
      showJUr(){//查看权限
        let mydata={
            myUserID:localStorage.userid,
            otherUserID:this.$store.state.userDetail.id
        }
        this.ylAjax.showJurisdiction(mydata)
                   .then(res=>{
                      if(res.state==0){
                          this.$store.commit("userState",res.data)
                      }else{
                          this.$store.commit("userState",[])
                      }
                   })
                   .catch(error=>{
                       throw error
                       this.$router.push({name:"userSet"})
                   })         
      },
      poi(){//位置权限
            let data={
            myUserID:localStorage.userid,
            openerID:this.$store.state.userDetail.id,
            permissionType:"位置"
        }
        if(this.position==false){//如果权限关闭，开启
            this.open(data)
        }else if(this.position==true){//如果开启,关闭 
            this.close(data)
        }
      },
      spo(){//运动权限
            let data={
            myUserID:localStorage.userid,
            openerID:this.$store.state.userDetail.id,
            permissionType:"运动"
        }
        if(this.sport==false){//如果权限关闭，开启
            this.open(data)
        }else if(this.sport==true){//如果开启,关闭 
            this.close(data)
        }
      },
      wea(){//环境
            let data={
            myUserID:localStorage.userid,
            openerID:this.$store.state.userDetail.id,
            permissionType:"环境"
        }
        if(this.weather==false){//如果权限关闭，开启
            this.open(data)
        }else if(this.weather==true){//如果开启,关闭 
            this.close(data)
        }        
      },
      hea(){//体征
            let data={
            myUserID:localStorage.userid,
            openerID:this.$store.state.userDetail.id,
            permissionType:"体征"
        }
        if(this.heart==false){//如果权限关闭，开启
            this.open(data)
        }else if(this.heart==true){//如果开启,关闭 
            this.close(data)
        } 
      },
      sle(){//睡眠
            let data={
            myUserID:localStorage.userid,
            openerID:this.$store.state.userDetail.id,
            permissionType:"睡眠"
        }
        if(this.sleep==false){//如果权限关闭，开启
            this.open(data)
        }else if(this.sleep==true){//如果开启,关闭 
            this.close(data)
        }
      },
      open(data){//开权限
        this.ylAjax.Jurisdiction(data)
            .then(res=>{
                if(res.state==0){
                }
            })
            .catch(error=>{
                throw error
            })
      },
      close(data){//关闭权限
        this.ylAjax.closeJurisdiction(data)
            .then(res=>{
                if(res.state==0){

                }else{

                }
            })
            .catch(error=>{

                throw error
            })

      },
  },
  mounted(){//进入页面判断
  if(this.userState.length>0){
        for(let i=0;i<this.userState.length;i++){
            if(this.userState[i].permissionType=="位置"){
                this.position=true
                this.positionid=this.userState[i].id
            }else if(this.userState[i].permissionType!=="位置"){
                // this.position=false
            }
            if(this.userState[i].permissionType=="环境"){
                this.weather=true
                this.weatherid=this.userState[i].id
            }else if(this.userState[i].permissionType!=="环境"){
                // this.weather=false
            }
            if(this.userState[i].permissionType=="运动"){
                this.sport=true
                this.sportid=this.userState[i].id
            }else if(this.userState[i].permissionType!=="运动"){
                // this.sport=false
            }
            if(this.userState[i].permissionType=="体征"){
                this.heart=true
                this.heartid=this.userState[i].id
            }else if(this.userState[i].permissionType!=="体征"){
                // this.heart=false
            }
            if(this.userState[i].permissionType=="睡眠"){
                this.sleep=true
                this.sleepid=this.userState[i].id
            }else if(this.userState[i].permissionType!=="睡眠"){
                // this.sleep=false
            }                                     
        }
  }else{
        this.position=false
        this.sport=false
        this.weather=false
        this.heart=false
        this.sleep=false
  }

      },
}
</script>
<style lang="less" scoped>
 @import "~less/base";
  .userSetContainer{//容器
    .top{//导航
        display: flex;
        height: 1rem;
        background-color: @background-a;
        line-height: 1rem;
        text-align: center; 
        color: @color-c;
        font-size: 14px;
        .left{
            flex: 0 0 30%;
           
        }
        .mid{
            flex: 0 0 50%;
            letter-spacing: 2px;
        }
    }
    .setList{//权限设置列表
        margin-top: .3rem;
        // padding: .2rem;
        .item{
            display: flex;
            height: 1.22rem;
            background-color: @background-bg;
            margin-top: .2rem;
            .left{
                flex: 0 0 80%;
                line-height: 1.22rem;
                font-size: 14px;
                padding-left: .5rem;
            }
            .right{
                flex: 0 0 20%;
                margin-top: .3rem;
            }
        }
    }
  }
</style>
